package com.gosling.cloudspace.tools;

//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClientBuilder;
//import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;

/**
 * @author Administrator
 */
public class HttpClientUtil {

    public static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    public static void postAddFile() {

//        //创HttpClient清求
//        CloseableHttpclient httpclient = Httpclients.createDefault();
//        String url ="https://frt.aaa.bbb:8080/test/oneServlet";
//        //创罐HttpPost
//        HttpPost httpPost = new HttpPost(url);
//        File file = new File("filePath");
//        //创罐上传文件的范到
//        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
//        entityBuilder.addTextBody("test","test");//添加普通参数
//        entityBuilder.addPart("fileName",new FileBody(file));//添p上传的件
//        HttpEntity httpEntity = entityBuilder.build();
//        httpPost.setEntity(httpEntity);
//        //执post清求
//        CloseableHttpResponse response = httpclient.execute(httpPost);

//        HttpRequest post = HttpUtil.createPost("xxxxx");
//        post.form("test","test");
//        Map<String, Resource> stringResourceMap = post.fileForm();
//        stringResourceMap.put("fileName", new FileResource(new File("C:\\Users\\EDY\\Desktop\\test.pdf")));
//
//        post.execute();

        String url = "https://xxx/file/upload";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("file", new File("D:/test.xls"));
        paramMap.put("signMsg", "xxxx");
        paramMap.put("platformNo", "xxxx");
        paramMap.put("apiName", "xxxx");
        paramMap.put("apiVersion", "xxxx");
        //请自行配置
        String result = cn.hutool.http.HttpRequest.post(url)
                //表单内容
                .form(paramMap)
                //超时，毫秒
                .timeout(20000)
                .execute()
                .body();
    }


//    private static CloseableHttpClient httpClient;
//
//    static {
//        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//        // 默认最大连接数是【50】，根据系统场景自定义设置
//        connectionManager.setMaxTotal(50);
//        // 每个路由默认最大并发连接数是【2】，根据系统场景自定义设置
//        connectionManager.setDefaultMaxPerRoute(2);
//        httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
//    }
//
//    public static CloseableHttpClient getHttpClient() {
//        return httpClient;
//    }
//
//    /**
//     * CloseableHttpClient是实现了Closeable接口和HttpClient接口的抽象类<br/>
//     * >>CloseableHttpClient关闭连接是关闭HttpClient连接池中的连接资源<br/>
//     * >>请求头【Connection】设置为【close】用于告诉服务器在响应完毕后关闭TCP连接<br/>
//     * 总结：为保证http请求能复用连接，需使用HttpClient连接池，且不能把请求头【Connection】设置为【close】，这样才能复用连接<br/>
//     */
//    public static void close() {
//        try {
//            httpClient.close();
//        } catch (IOException e) {
//            logger.error(e.getMessage() ,e);
//            throw new RuntimeException(e);
//        }
//    }
}